package com.kang.api;

import com.kang.service.WeixinApiService;
import com.kang.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Api(tags = "微信")
@Controller
@RequestMapping("/admin/api/ucenter/wx")
@Slf4j
public class WeixinApiController {

    @Autowired
    private WeixinApiService weixinApiService;

    /**
     * 获取微信登录参数
     */
    @ApiOperation("微信扫码获取参数")
    @GetMapping("getLoginParam")
    @ResponseBody
    public WebResponse genQrConnect() {
        try {
            return WebResponse.resSuccess("微信扫码获取参数成功",weixinApiService.genQrConnect());
        } catch (Exception ex) {
            log.info("微信扫码获取参数失败",ex);
            return WebResponse.resFail("微信扫码获取参数失败",null);
        }
    }

    @ApiOperation("扫码回调")
    @GetMapping("callback")
    @ResponseBody
    public void callback(String code, String state,HttpSession session)  {
        try {
            weixinApiService.callback(code,state);
        } catch (Exception ex) {
            log.info("扫码回调失败",ex);
        }
    }

    /**
     * 根据状态判断是否扫码
     * @param state
     * @return
     */
    @ApiOperation("判断是否扫码")
    @GetMapping("getStatus/{state}")
    @ResponseBody
    public WebResponse getStatus(@PathVariable("state") String state) {
        try {
            return WebResponse.resSuccess("判断是否扫码成功",weixinApiService.getStatus(state));
        } catch (Exception ex) {
            log.info("判断是否扫码异常",ex);
            return WebResponse.resFail("判断是否扫码异常",null);
        }
    }
}
